#pragma once

#include "uicontrol.h"
#include "graphics.h"

class MasterPanel;

class Panel : public UIControl
{
protected:
	SRect m_dimension;
	Panel * m_parent;
public:
	inline void setParent(Panel * a_parent){m_parent=a_parent;}
	inline Panel * getParent(){return m_parent;}
	Panel():m_parent(0){}
	virtual SRect * getArea()
	{
		return &m_dimension;
	}
	virtual void setArea(const SRect & a_dimension)
	{
		m_dimension.set(a_dimension);
	}
	virtual void refreshArea(){}
	/** have the panel update its own area (perhaps based on content, or parent size) */
	virtual bool update(const int & a_ms)
	{
		return false;
	}
	inline void copy(const Panel & b)
	{
		setArea(b.m_dimension);
	}
	virtual void setDirty(SDL_Surface * a_surface)
	{
		// get the area this this element would like to make dirty
		SRect area(*getArea());
		// now, make sure it is valid within parent boundaries...
		Panel * p = m_parent;
		while(p && area.isValid())
		{
			area.clip(*p->getArea());
			p = p->m_parent;
		}
		if(area.isValid())
		{
			//VSDL_addDirtyRectangle(a_surface, area);
			addDirtyRectangle(area);
		}
	}
	MasterPanel * getMasterPanel()
	{
		Panel * p = this;
		while(p->getParent())
		{
			p = p->m_parent;
		}
		if(!p->isMasterPanel())
		{
			printf("uh oh, non master panel found at root of UI tree\n");
		}
		return (MasterPanel*)p;
	}
	/** @return true if this is the root panel of the entire UI system */
	virtual bool isMasterPanel(){return false;}

	/** @return number of dirty rectangle, asks the root UI panel of the UI system */
	int getDirtyRectangleCount();
	/** @return a specific dirty rectangle, asks the root UI panel of the UI system */
	SRect * getDirtyRectangle(const int & a_index);
	/** adds a dirty rectangle to the Dirty Rectangle system of the root UI panel */
	void addDirtyRectangle(const SRect & a_dirtyArea);
};